
#include "InfoDetailUI.h"
#include "DialogManager.h"
#include "CardUI.h"
#include "PokerLogic.h"

USING_NS_CC;
USING_NS_CC_EXT;

// 数据备份，用于战局保存和回放
uint8_t rec_bose; // 谁是地主
std::vector< std::tuple<uint8_t, std::vector< int32_t >, std::vector< int32_t > > > rec_out; // 每一轮出牌的信息

CustomTableViewCell::CustomTableViewCell() : maskColorLayer(nullptr) {
}

void CustomTableViewCell::setIdx(ssize_t uIdx)
{
    TableViewCell::setIdx(uIdx);
    if(m_theHandCard.empty() || m_theOutCard.empty()) {
        Color4B bgColor(150, 150, 150, 255);
        this->maskColorLayer = LayerColor::create(bgColor, 1035, 3);
        this->addChild(maskColorLayer);
        // 手牌展示区建立缓存, 最多20张牌, 直接丢20个UI
        for (int i = 0; i < 20; ++i) {
            CardUI* ui = CardUI::create();
            ui->disableTouch();
            ui->setPosition(Vec2(i * 30.0f - 160.0f, -130.0f));
            ui->setLocalZOrder(i);
            ui->setScale(0.45);
            // 牌面：3, 4, 5, 6, 7, 8, 9, 10, J, Q,  K,  A,  2, XW, DW
            // 索引：0, 1, 2, 3, 4, 5, 6, 7,  8, 9, 10, 11, 12, 13, 14
            if(i > 12) {
                ui->setData((int32_t)(100 * (1 + i / 12) + (i % 12)));
            } else {
                ui->setData((int32_t)(100 + i));
            }
            this->addChild(ui);
            this->m_theHandCard.emplace_back(ui);
            //-----------------------------------------
            ui = CardUI::create();
            ui->disableTouch();
            ui->setPosition(Vec2(i * 20.0f - 260.0f, -100.0f));
            ui->setLocalZOrder(i);
            ui->setScale(0.3);
            // 牌面：3, 4, 5, 6, 7, 8, 9, 10, J, Q,  K,  A,  2, XW, DW
            // 索引：0, 1, 2, 3, 4, 5, 6, 7,  8, 9, 10, 11, 12, 13, 14
            if(i > 12) {
                ui->setData((int32_t)(100 * (1 + i / 12) + (i % 12)));
            } else {
                ui->setData((int32_t)(100 + i));
            }
            this->addChild(ui);
            this->m_theOutCard.emplace_back(ui);
            //-----------------------------------------
            //lblSeat, lblOut, lblHand;
            lblSeat = Label::createWithSystemFont("   ", "Helvetica", 20.0);
            lblSeat->setPosition(Vec2(0.0f, 85.0f));
            lblSeat->setAnchorPoint(Vec2::ZERO);
            lblSeat->setTextColor(Color4B::BLUE);
            this->addChild(lblSeat);
            lblOut = Label::createWithSystemFont("   ", "Helvetica", 20.0);
            lblOut->setPosition(Vec2(60.0f, 130.0f));
            lblOut->setAnchorPoint(Vec2::ZERO);
            lblOut->setTextColor(Color4B::BLACK);
            this->addChild(lblOut);
            lblHand = Label::createWithSystemFont("   ", "Helvetica", 20.0);
            lblHand->setPosition(Vec2(60.0f, 40.0f));
            lblHand->setAnchorPoint(Vec2::ZERO);
            lblHand->setTextColor(Color4B::BLACK);
            this->addChild(lblHand);
        }
    }
    uint8_t master = PokerLogic::shard()->getMasterSeat();
    // std::tuple<uint8_t, std::vector< int32_t > >
    auto& data = rec_out[uIdx];
    uint8_t seat = std::get<0>(data);
    std::vector< int32_t >& theCardsOut = std::get<1>(data);
    std::vector< int32_t >& theCardsHand = std::get<2>(data);
    lblSeat->setString(master == seat ? "👲 地主" : "👩‍🌾 农民");
    // 出牌
    auto index = 0;
    for(auto&& card : theCardsOut) {
        m_theOutCard[index]->setVisible(true);
        m_theOutCard[index]->setData(card);
        ++index;
    }
    for(; index < 20; ++index) {
        m_theOutCard[index]->setVisible(false);
    }
    if(theCardsOut.empty()) {
        lblOut->setString("出牌 →    (不出)");
    } else {
        lblOut->setString("出牌 →");
    }
    // 手牌
    index = 0;
    for(auto&& card : theCardsHand) {
        m_theHandCard[index]->setVisible(true);
        m_theHandCard[index]->setData(card);
        ++index;
    }
    for(; index < 20; ++index) {
        m_theHandCard[index]->setVisible(false);
    }
    lblHand->setString("手牌 →");
}

InfoDetailUI::InfoDetailUI() : IDialog() {
    RegDialogCtrl("close", m_close);
}

InfoDetailUI::~InfoDetailUI() {
}

void InfoDetailUI::onUILoaded() {
    this->m_close->addClickEventListener([this](Ref* sender) {
        DialogManager::shared()->closeCurrentDialog();
    });
    Size winSize = Director::getInstance()->getWinSize();
    TableView* tableView = TableView::create(this, Size(1038, 450));
    tableView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
    tableView->setPosition(Vec2(80,70));
    tableView->setDelegate(this);
    tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
    this->addChild(tableView, 10000);
    tableView->reloadData();
    this->m_close->setGlobalZOrder(20000);
}

void InfoDetailUI::tableCellTouched(TableView* table, TableViewCell* cell) {
    CCLOG("cell touched at index: %ld", static_cast<long>(cell->getIdx()));
}

Size InfoDetailUI::tableCellSizeForIndex(TableView *table, ssize_t idx) {
    return Size(1035, 170);
}

TableViewCell* InfoDetailUI::tableCellAtIndex(TableView *table, ssize_t idx) {
    auto string = StringUtils::format("%ld", static_cast<long>(idx));
    TableViewCell *cell = table->dequeueCell();
    if (!cell) {
        cell = new (std::nothrow) CustomTableViewCell();
        cell->autorelease();
        cell->setIdx(idx);
    }
    return cell;
}

ssize_t InfoDetailUI::numberOfCellsInTableView(TableView *table) {
    return rec_out.size();
}

